/**
 * Created by wangyu
 */
import React from 'react';
import { message, notification, Button} from 'antd';
import { Modal} from '@/components/wocloud-antd';
import { ExclamationCircleFilled} from '@ant-design/icons';
import {helper, auth} from "./index";
import EnumRouter from "@/config/EnumRouter";
import {childConfig} from "@/config";

class Prompt {


  constructor() {
    message.config({
      top: 80,
      duration: 2,
      maxCount: 3,
      prefixCls: childConfig.prefixCls + '-message',
    });

    notification.config({
      top: 52,
    });

    // 统一对message的methods添加默认配置
    Object.entries(message).forEach(([key, method]) => {
      if (method instanceof Function) {
        message[key] = function (...args: any[]) {
          if (args[0] instanceof Object) {
            // 如果返回的数据为空则不提示
            if (args[0] && args[0].content) {
              return method(...args);
            }
            return null;
          }
          return method(...args);
        };
      }
    });
  }

  /**
   * 通知提示框
   * @param message
   * @param description
   * @param config
   */
  notificationOpen(message: string, description: string, config = {}) {
    notification.open({
      message: message || '提示',
      description: description || '一条新消息',
      duration: null,
      ...config,
    });
  }

  /**
   * 通知提示框api
   */
  notificationApi() {
    return notification;
  }

  /**
   * 提示成功
   * @param {String} msg
   * @param {Number} duration
   * @param {Function} onClose
   */
  systemNotice(msg: string, duration = 2, onClose = () => {
  }) {
    message.info(msg, duration, onClose);
  }

  /**
   * 提示成功
   * @param {String} msg
   * @param {Number} duration
   * @param {Function} onClose
   */
  success(msg: string, duration = 2, onClose = () => {
  }) {
    message.success(msg, duration, onClose);
  }

  /**
   * 提示错误
   * @param {String} msg
   * @param {Number} duration
   * @param {Function} onClose
   */
  error(msg: string, duration = 2, onClose = () => {
  }) {
    message.error(msg, duration, onClose);
  }

  /**
   * 提示警告
   * @param {String} msg
   * @param {Number} duration
   * @param {Function} onClose
   */
  warn(msg:string, duration = 2, onClose = () => {
  }) {
    message.warn(msg, duration, onClose);
  }

  /**
   * 确认提示框
   * @param {Function} cbForOk return Promise对象
   * @param {Object} options
   */
  confirm(cbForOk:Function, options = {}) {
    options = Object.assign({
      title: '确定删除吗？',
      // content: <h1>When clicked the OK button, this dialog will be closed after 1 second</h1>,
      content: '',    // content可以是react节点实例
      okText: '确定',
      cancelText: '取消',
      onOk() {
        return cbForOk();
      },
      onCancel() {
        return null;
      },
      getContainer: () => {
        const root = document.getElementById(childConfig.rootId);
        if (root) {
          return root;
        }
        return document.body;
      },
    }, options);


    return Modal.confirm(options);
  }

  /**
   * 进行全局 loading，异步自行移除
   * @param msg
   * @returns {MessageType}
   */
  loading(msg:string) {
    return message.loading(msg, 0);
  }

  /**
   * 消息弹窗提示
   * @param option
   */
  messageModal(option = {}) {
    Modal.warning(Object.assign({
      title: '消息提示',
      content: '消息提示',
    }, option));
  }


  /**
   * 登录过期，手动跳转
   */
  setLogout(option = {}) {
    let defaultOption = Object.assign({
      title: '登录失效，请重新登录',
      okText: '重新登录',
    }, option);

    Modal.destroyAll();
    const ModalBox = <Modal
      visible={true}
      title={'提示'}
      centered
      width={400}
      cancelButtonProps={{
        hidden: true,
      }}
      closable={false}
      footer={[
        <Button
          key={1}
          prefixCls={childConfig.prefixCls + '-btn'}
          type={'primary'}
          onClick={() => {
            console.log(window.location.href);
            auth.clearLoginState();
            helper.redirect(EnumRouter.login + '?status=10201');
            Modal.destroyAll();
          }}
        >{defaultOption.okText}</Button>,
      ]}
    >
      <p><ExclamationCircleFilled style={{color: '#e6a23c'}}/> {defaultOption.title}</p>
    </Modal>;

    helper.renderModal(ModalBox);
  }
}

export default new Prompt();
